﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jayden.Dll.BitTorrent
{
	public class BitTorrentPiece
	{
		private BitTorrentFileManager m_File;
		private int m_PieceIndex;
		private List<BitTorrentBlock> m_Blocks;

		public BitTorrentPiece(BitTorrentFileManager file, int index)
		{
			m_File = file;
			m_PieceIndex = index;
			CreateBlocks();
		}
		protected void CreateBlocks()
		{
			if (m_Blocks == null)
			{
				m_Blocks = new List<BitTorrentBlock>();
				int blockIndex = 0;
				while (blockIndex < PieceLength)
				{
					BitTorrentBlock block = new BitTorrentBlock(this, blockIndex, BitTorrentBlock.DefaultBlockLength);
					blockIndex += BitTorrentBlock.DefaultBlockLength;
					m_Blocks.Add(block);
				}
			}
		}

		public int PieceLength { get { return m_File.GetPieceLength(m_PieceIndex); } }
	}
}
